package com.example.myapplication1.loldemo;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapplication1.R;
import com.example.myapplication1.adapter.FruitAdapter;
import com.example.myapplication1.bean.Fruit;
import com.example.myapplication1.threademo.conditiontest.BoundedBuffer;
import com.example.myapplication1.threademo.productinterface.Custom;
import com.example.myapplication1.threademo.productinterface.Product;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.Exchanger;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Copyright (C), 2020, 重庆八进制有限公司
 *
 * @author: weng
 * @last-modifier: weng
 * @version: 1.0
 * @create-date: 2020/11/23 15:01
 * @last-modify-date: 2020/11/23 15:01
 * @description:
 */
public class LolRecordQueryActivity extends Activity {

    private static final String TAG = "LolRecordQueryActivity:";
    private List<Fruit> fruitList = new ArrayList<>();

    private RecyclerView recycleview;


    private int MAX_COUNT = 10;
    //    目前的数量
    private int currentCount;
    //    锁
//    private Object object = new Object();


//    mast分支


    private ReentrantLock lock = new ReentrantLock();
    private Condition conditionA = lock.newCondition();
    private Condition conditionB = lock.newCondition();


    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_lol_record1);
//        recycleview = findViewById(R.id.recycleview);
//        binding = ActivityLolRecord1Binding.inflate(getLayoutInflater());
//        setContentView(binding.getRoot());
//        initFruits();
//        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
//        binding.recycleview.setLayoutManager(layoutManager);
//        FruitAdapter adapter = new FruitAdapter(getApplicationContext(), fruitList);
//        binding.recycleview.setAdapter(adapter);
//        binding.recycleview.setNestedScrollingEnabled(true);
////        test();
////        testCyclicBarrier();
//
//        ExchangeerTest exchangeerTest = new ExchangeerTest();
//        ExchangeerTest1 exchangeerTest1 = new ExchangeerTest1();
//        exchangeerTest.start();
//        exchangeerTest1.start();
//        binding.headercontainer.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                binding.headercontainer.setTranslationY(20);
//            }
//        });
    }
// devp
// devp1
//    devp2



    private void initFruits() {

        for (int i = 0; i < 2; i++) {
            Fruit apple = new Fruit("Apple", R.drawable.ic_launcher_background);
            fruitList.add(apple);
            Fruit banana = new Fruit("Banana", R.drawable.ic_launcher_background);
            fruitList.add(banana);
            Fruit orange = new Fruit("Orange", R.drawable.ic_launcher_background);
            fruitList.add(orange);
            Fruit watermelon = new Fruit("Watermelon", R.drawable.ic_launcher_background);
            fruitList.add(watermelon);
            Fruit pear = new Fruit("Pear", R.drawable.ic_launcher_background);
            fruitList.add(pear);
            Fruit grape = new Fruit("Grape", R.drawable.ic_launcher_background);
            fruitList.add(grape);
            Fruit pineapple = new Fruit("Pineapple", R.drawable.ic_launcher_background);
            fruitList.add(pineapple);
            Fruit strawberry = new Fruit("Strawberry", R.drawable.ic_launcher_background);
            fruitList.add(strawberry);
            Fruit cherry = new Fruit("Cherry", R.drawable.ic_launcher_background);
            fruitList.add(cherry);
            Fruit mango = new Fruit("Mango", R.drawable.ic_launcher_background);
            fruitList.add(mango);
        }

    }


    public void test() {

//        for (int i = 0; i < 10; i++) {
//        Customer customer = new Customer();
//        customer.start();
//        }

//        for (int i = 0; i < 20; i++) {
//        Producter product = new Producter();
//        product.start();
//        }


        final BoundedBuffer boundedBuffer = new BoundedBuffer();
        new Thread() {
            @Override
            public void run() {
                super.run();
                boundedBuffer.put(new Object());
            }
        }.start();
        new Thread() {
            @Override
            public void run() {
                super.run();
                boundedBuffer.take();
            }
        }.start();
    }


    public class Customer extends Thread implements Custom {

        @Override
        public void custom() {
//  消费者 如果目前的数量小于0 ,就等待,否者就消费一个

            while (true) {
                try {
//                    test
                    lock.lock();
                    try {
                        sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    while (currentCount <= 0) {
                        Log.e("tag", getName() + "--目前没有可消费的苹果,等待");
                        conditionA.await();
                    }
                    currentCount--;
                    conditionB.signal();
                    Log.e("tag", getName() + "--消费者消费了1个苹果,剩余" + currentCount + "个苹果");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {

                    lock.unlock();
                }
            }
        }

        @Override
        public void run() {
            super.run();
            custom();
        }
    }

    public class Producter extends Thread implements Product {


        public void product() {
//             生产者 如果目前的苹果大于最大数量，等待消费者消费后在生产，如果生产的苹果加上剩下的苹果大于了最大数量，应该就生产最大数量
            while (true) {

                try {
                    lock.lock();
                    try {
                        sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    while (currentCount >= MAX_COUNT) {
                        Log.e("tag", getName() + "--生产的苹果已经达到最大数量了，等消费者消费");
                        conditionB.await();
                    }

                    int productCount = (int) (Math.random() * (MAX_COUNT - currentCount));
                    currentCount = currentCount + productCount;
                    conditionA.signal();
                    Log.e("tag", getName() + "--生产了" + productCount + "个苹果,总的苹果为" + currentCount + "个");

                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }
            }

        }

        @Override
        public void run() {
            super.run();
            product();

        }
    }

    private CyclicBarrier cyclicBarrier;

    public void testCyclicBarrier() {

        cyclicBarrier = new CyclicBarrier(3);
        CyclicBarrierTest cyclicBarrierTest1 = new CyclicBarrierTest();
        CyclicBarrierTest cyclicBarrierTest2 = new CyclicBarrierTest();
        CyclicBarrierTest cyclicBarrierTest3 = new CyclicBarrierTest();
        cyclicBarrierTest1.start();
        cyclicBarrierTest2.start();
        cyclicBarrierTest3.start();
    }


    public class CyclicBarrierTest extends Thread {
        @Override
        public void run() {

            Log.e("tag", getName() + "--到达栅栏A");
            try {
                cyclicBarrier.await();
            } catch (BrokenBarrierException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Log.e("tag", getName() + "--冲出栅栏A");

            Log.e("tag", getName() + "--到达栅栏B");
            try {
                cyclicBarrier.await();
            } catch (BrokenBarrierException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Log.e("tag", getName() + "--冲出栅栏B");
        }
    }


    private Exchanger<String> exchanger = new Exchanger();
    private String exchange = "exchangeA";
    public class ExchangeerTest extends Thread {

        @Override
        public void run() {

            try {
                String exchangea=  exchanger.exchange("exchangeA");
                Log.e("tag","ExchangeerTest"+"exchange--->"+exchangea);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }


        }
    }

    public class ExchangeerTest1 extends Thread {

        @Override
        public void run() {

            try {
              String   exchangeb= exchanger.exchange("exchangeB");
                Log.e("tag","ExchangeerTest1"+"exchange--->"+exchangeb);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }
}
